﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QFileDialog"
#include "QMessageBox"
#include "QDate"

#include "result.h"

extern result *relt;
bool ProductionID_flag = false;
QString ProductionID;

QString manufacture;
QString result_dir;

MainWindow::MainWindow(QWidget *parent) :
	QMainWindow(parent),
	ui(new Ui::MainWindow)
{
	ui->setupUi(this);
	static const QDate buildDate = QLocale( QLocale::English ).toDate( QString( __DATE__ ).replace( "  ", " 0" ), "MMM dd yyyy");
	QString tatil = "KFT."+ buildDate.toString("yyyy_MM_dd");
	this->setWindowTitle(tatil);

	QList<QSerialPortInfo> infolist;
	QList<QSerialPortInfo>::iterator iter;
	QSerialPortInfo     info;

	infolist = info.availablePorts();
	for(iter=infolist.begin(); iter != infolist.end(); iter++){
		ui->comboBox_com->addItem(iter->portName());

	}

	tools = new Command(ui->widget_2);
	script = new Script_thread(tools,0);
	consol = new console(ui->widget,script);

	script->start();


	start_all_flag = false;

	QFile file("cfg.ini");
	if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
		qDebug()<<"Can't open the cfg file!"<<endl;
	}
	while(!file.atEnd()){
		QByteArray tmp = file.readLine();
		QString line(tmp);
		QStringList list = line.split('=',QString::SkipEmptyParts);
		if(list[0] == "manufacture"){
			manufacture = list[1];
		}

	}
	qDebug()<<"manufacture:"<<manufacture<<endl;

}

MainWindow::~MainWindow()
{
	delete tools;
	delete consol;
	delete ui;
}



void openFileDialog(QLineEdit*& widget, int alwaysTextChanged)
{
	QString filters("Excel(*.xlsx)");
	QString startpath = QDir::currentPath();
	if(widget->text() != "")
		startpath = widget->text();
	QFileDialog fileDialog(0, "Select file", startpath, filters);
	fileDialog.exec();

	if(fileDialog.result() == QDialog::Accepted)
	{
		QStringList list = fileDialog.selectedFiles();
		if(list.isEmpty()){
			return;
		}

		QString sf = list.at(0);
		qDebug()<<"ddd:"<<sf;
		if(sf.indexOf(".xlsx") == -1){
			sf.append(".xlsx");
		}
		widget->setText(sf);
		if(alwaysTextChanged != 0)
			emit widget->returnPressed();
	}
}

void MainWindow::on_pushButton_clicked()
{
#if 1
	result_dir = QFileDialog::getExistingDirectory(0, "Open Directory",
			ui->lineEdit_output->text(),
			QFileDialog::ShowDirsOnly
			| QFileDialog::DontResolveSymlinks);
	if(result_dir != NULL)
	{
		//ui->lineEdit_output->setText(dir);
	}
#endif
	//openFileDialog(ui->lineEdit_output,1);
}

void MainWindow::on_lineEdit_output_textChanged(const QString &arg1)
{
	relt->Set_filename(ui->lineEdit_output->text());
}



void MainWindow::on_pushButton_startall_clicked()
{

	qDebug()<<"main pushbutton startall"<<endl;


	int i;
	start_all_flag = !start_all_flag;
	if(start_all_flag){

		ui->pushButton_startall->setStyleSheet(BK_RED);
        QString str = QString("停止");
		ui->pushButton_startall->setText(str);
		QString qrate = ui->comboBox_rate->currentText();
		int rate = qrate.toInt();
		this->consol->start_test(true,ui->comboBox_com->currentText(),rate);
	}else{
		ui->pushButton_startall->setStyleSheet(BK_GREEN);
        QString str1 = QString("启动");
		ui->pushButton_startall->setText(str1);
		this->consol->start_test(false,NULL,0);

	}
}

